// pages/mineModule/sport/sport.js
import request from '../../../utils/request'
var util = require('../../../utils/util.js')
var waterList = [];
Page({

  /**
   * 页面的初始数据
   */
  data: {
    todayTime:"",
    textareaDisabled:true,
    textContent:"",
    isFinishSwitch:false,
    runSwitch:false,
    emptyWater:"../../../images/emptyWater.png",
    fullWater:"../../../images/fullWater.png",
    waterList:[],
    sports:{
      exercisePlan:"",
      isFinish:false,
      isRun:false,
      waterNum:0
    }
  },

  getMySport:function() {
    var that=this;
    var dayTime = util.formatTime(new Date());
    var dayTimes=dayTime.split(" ")[0].split("/").join("-")
    request.get('/selectSportByOpenid',{exerciseDate:dayTimes}).then(res => {
      console.log(res.data)
      if(res.data){
        this.setData({
          sports:{
            exercisePlan:res.data.exercisePlan,
            isFinish:res.data.isFinish,
            isRun:res.data.isRun,
            waterNum:res.data.waterNum
          }
        })
        var num =this.data.sports.waterNum
        for(var i=0;i<num;i++){
          waterList.push({
            isWater:true
          })
        }
        for(var i=0;i<8-num;i++){
          waterList.push({
            isWater:false
          })
        }
        this.setData({
          waterList
        })
      }else{
        var data={
          exerciseDate:dayTimes,
          exercisePlan:"",
          isFinish:0,
          isRun:0,
          waterNum:0
        }
        request.post("/insertSport", data).then(res => {
          console.log(res);
        })
        for(var i=0;i<8;i++){
          waterList.push({
            isWater:false
          })
        }
        this.setData({
          waterList
        })
        console.log( this.data.waterList)
      }
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    waterList = [];
    var dayTime = util.formatTime(new Date());
    var dayTimes=dayTime.split(" ")[0].split("/")
    var today=dayTimes[0]+"年"+dayTimes[1]+"月"+dayTimes[2]+"日"
    this.setData({
      todayTime:today
    })
    this.getMySport()
  },
  editingStatus:function(){
    var that=this;
    that.setData({
      textareaDisabled:false
    })
  },
  finishStatus:function(){
    var that=this;
    that.setData({
      textareaDisabled:true
    })
    var dayTime = util.formatTime(new Date());
    var dayTimes=dayTime.split(" ")[0].split("/").join("-")
    var data={
      exerciseDate:dayTimes,
      exercisePlan:that.data.sports.exercisePlan
    }
    request.post("/updateSportByOpenid", data).then(res => {
      console.log(res);
    })
  },
  textinput:function(e){
    var content = e.detail.value;
    this.data.sports.exercisePlan = content
  },
  isFinishSwitchChange: function (e){
    var dayTime = util.formatTime(new Date());
    var dayTimes=dayTime.split(" ")[0].split("/").join("-")
    var data={
      exerciseDate:dayTimes,
      isFinish:(e.detail.value)?1:0
    }
    request.post("/updateSportByOpenid", data).then(res => {
      console.log(res);
    })
  },
  runSwitchChange: function (e){
    var dayTime = util.formatTime(new Date());
    var dayTimes=dayTime.split(" ")[0].split("/").join("-")
    var data={
      exerciseDate:dayTimes,
      isRun:(e.detail.value)?1:0
    }
    request.post("/updateSportByOpenid", data).then(res => {
      console.log(res);
    })
  },
  changeEmptyWater:function(e){
    var that=this
    var flag;
    var dayTime = util.formatTime(new Date());
    var dayTimes=dayTime.split(" ")[0].split("/").join("-")
    if(that.data.waterList[e.currentTarget.id].isWater){
      flag=false
    }else{
      flag=true
    }
    var up = "waterList[" + e.currentTarget.id + "].isWater"
    that.setData({
      [up]:flag
    })
    var num=0;
    for(var i=0;i<8;i++){
      if(that.data.waterList[i].isWater){
        num++
      }
    }
    var data={
      exerciseDate:dayTimes,
      waterNum:num
    }
    request.post("/updateSportByOpenid", data).then(res => {
      console.log(res);
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})